//
//  EPMUserAccountModelView.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/3/23.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMUserAccountModelView.h"

@interface EPMUserAccountModelView()
    
@property (assign,nonatomic) BOOL isExperis;
@end
@implementation EPMUserAccountModelView

+ (instancetype)sheardTool
    {
        static EPMUserAccountModelView* tool;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            tool = [EPMUserAccountModelView new];
        });
        return tool;
    }
  

    
    - (instancetype)init {
        
        if(self = [super init])
        {
            self.account = [self loadUserAccount];
        }
        return self ;
        
    }
   
    - (NSString*)accessToken{
        return self.account.access_token;
    }
    
    - (NSString*)headURL
    {
        return self.account.avatar_large;
    }
    
    
    - (BOOL)userLogin
    {
        if ((self.account.access_token != nil) && !self.isExperis)
        {
            return  true;
        }
        return false;
    }
    //判断是否过期
    -(BOOL)isExperis{
        
        if ([self.account.expires_date compare:[NSDate date]] == NSOrderedDescending ){
            return false;
        }
        return true;
    }

    
    
    
    
#pragma
#pragma-获取token
    - (void)geTokenWithCode:(NSString*)code finished:(void(^)(bool))finishedBlock{
        NSString* URLString =  @"https://api.weibo.com/oauth2/access_token";
        NSDictionary* parameter = @{ @"client_id" : client_id,
                                     @"client_secret": client_secret,
                                     @"grant_type":@"authorization_code",
                                     @"code":code,
                                     @"redirect_uri":redirect_uri
                                    };
       [ [EPMNetWorkTool sheardTool] request: POST url:URLString paramater:parameter finisedBlock:^(id reasult, NSError *error) {
           if ( error == nil){
               
               NSDictionary* dict = reasult;
//               NSLog(@"%@",dict);
               [self loadUserInfo:dict finished:finishedBlock];
           }else{
               finishedBlock(NO);
           }
       }];
        
    }
    
#pragma
#pragma-获取网络数据
    - (void)loadUserInfo:(NSDictionary*)dict finished:(void(^)(bool))finishedBlock{
        //请求用户数据
        NSString* urlString = @"https://api.weibo.com/2/users/show.json";
        //请求参数
        NSDictionary* parameter = @{@"access_token":dict[@"access_token"],
                                    @"uid":dict[@"uid"]
                                    };
        [[EPMNetWorkTool sheardTool] request:GET url:urlString paramater:parameter finisedBlock:^(id res, NSError* error) {
            if ( error != nil){
                finishedBlock(NO);
                return;
            }
            NSDictionary* tempDict = res;
          
            NSMutableDictionary* userInfoDict = [NSMutableDictionary new];
            [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                [userInfoDict setValue:obj forKey:key];
            }];
            [tempDict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                [userInfoDict setValue:obj forKey:key];
            }];
            
            
            
            EPMAccount* model = [EPMAccount new];
//            NSLog(@"%@",model);
            
            [model setValuesForKeysWithDictionary:userInfoDict];
            self.account = model;
            [self saveAccount:model];
            //加载成功
            finishedBlock(YES);
            
        }];
        
    }
    
    
    ///数据获取
    - (EPMAccount*)loadUserAccount{
        EPMAccount* acc = [NSKeyedUnarchiver unarchiveObjectWithFile:FILEPATH];
        NSLog(@"%@",acc);
        return acc;
    }
    ///数据存储
    - (void)saveAccount:(EPMAccount*) account{
        
        [NSKeyedArchiver archiveRootObject:account toFile:FILEPATH];
        
    }
@end


